package com.aduerr.v1shouyin.print;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.aduerr.v1shouyin.App;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.entity.MemberConsumeDetailEntity;
import com.aduerr.v1shouyin.entity.MemberRechargeDetailEntity;
import com.aduerr.v1shouyin.entity.OrderGetPosOrderLEntity;
import com.aduerr.v1shouyin.entity.PreAuthoOrderEntity;
import com.aduerr.v1shouyin.util.DayWeekMoonTImeUtil;
import com.aduerr.v1shouyin.util.NumUtil;
import com.aduerr.v1shouyin.util.Utils;
import com.orhanobut.hawk.Hawk;
import com.yeahka.shouyintong.sdk.Constant;
import com.yeahka.shouyintong.sdk.action.PrinterTicket;
import com.yeahka.shouyintong.sdk.api.SytFactory;
import com.yeahka.shouyintong.sdk.info.TradeInfo;
import com.yeahka.shouyintong.sdk.printer.IPrinterContent;
import com.yeahka.shouyintong.sdk.printer.PrinterContent;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import cn.bertsir.zbar.utils.QRUtils;

public class PrinterWrapper {

    private static final String TAG = PrinterWrapper.class.getSimpleName();



    public static void print(PrinterContent printer) {
        PrinterTicket.Req p = new PrinterTicket.Req();
        p.printerContent = printer;
        SytFactory.createSytIml(App.getApp()).sendReq(p);
    }

    /**
     * 银联订单打印
     *
     * @param logo
     * @param tradeInfo
     * @return
     */
    public static void printUnionOrder(final Bitmap logo, final TradeInfo tradeInfo) {

        printUnionOrder(logo, tradeInfo, false);
    }


    /**
     *
     * @param logo
     * @param tradeInfo
     * @param isSecond 是否是补打小票
     * @return
     */
    public static void printUnionOrder(final Bitmap logo,
                                       final TradeInfo tradeInfo,
                                       final boolean isSecond) {
        printCardOrder(logo, tradeInfo, isSecond);
    }

    private static void printCardOrder(final Bitmap logo, final TradeInfo tradeInfo,
                                       final boolean isSecond) {

        PrinterContent printer = new PrinterContent();
        if(logo != null) {
            printer.printBitmap(logo);
        }
        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        if(isSecond) {
            printer.printlnText("补打交易凭证");
        } else {
            printer.printText("存根", "请妥善保管");
        }
        printer.printDividerLine();
        printer.printlnText("商户名称:" + tradeInfo.getMerchantName());
        printer.printlnText("MERCHANT NAME");
        printer.printlnText("门店名称:" +  Hawk.get("shopname"));
        printer.printlnText("STORE NAME");
//        printer.printlnText("商户编号:" + tradeInfo.yeahkaMerchantId());
        printer.printlnText("MERCHANT NO.");
        printer.printlnText("发卡行:" + tradeInfo.getBankName());
        printer.printlnText("ISS");
        printer.printlnText("卡号:(CARD NO)");
        printer.printlnText(tradeInfo.getCardNo().replaceAll("(\\d{6})\\d+(\\d{4})",
                "$1******$2"));
        String validity = tradeInfo.getCardValidity();
        String cardValidity = validity != null ?
                ("20" + validity.substring(0, 2) + "/" + validity.substring(2, 4)) : "";
        printer.printlnText("日期:" + Utils.dateToStrLong(tradeInfo.getCreateTimeStamp()));
        printer.printlnText("TRADE TIME");
        printer.printlnText("有效期:" + cardValidity);
        printer.printlnText("EXP DATE");

        printer.bold();
        printer.printlnText("交易类型:" + (tradeInfo.getTransactionType() == Constant.TRANSACTION_TYPE_CONSUME ? "消费(SALE)" : "退货(REFUND)"));
        printer.cancelBold();

        printer.printlnText("TRANS TYPE");
        printer.printText("参考号:" + tradeInfo.getReferenceNo(),
                IPrinterContent.ALIGNMENT_LEFT,
                "批次号:" + tradeInfo.getBatchNo(), IPrinterContent.ALIGNMENT_LEFT);
        printer.printText("REFER NO.", IPrinterContent.ALIGNMENT_LEFT, "BATCH NO.",
                IPrinterContent.ALIGNMENT_LEFT);
        printer.printText("授权码:" + tradeInfo.getAuthorizedResponseCode(),
                IPrinterContent.ALIGNMENT_LEFT, "凭证号:" + tradeInfo.getFlowId(),
                IPrinterContent.ALIGNMENT_LEFT);
        printer.printText("AUTH NO.", IPrinterContent.ALIGNMENT_LEFT, "VOUCHER NO",
                IPrinterContent.ALIGNMENT_LEFT);

        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.bold();
        printer.printText("金额:" + Utils.getAmountString(tradeInfo.getAmount()),
                IPrinterContent.ALIGNMENT_LEFT, "RMB", IPrinterContent.ALIGNMENT_LEFT);
        printer.cancelBold();
        printer.printlnText("AMOUNT");
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_SMALL);
//        if(!isSecond) {
//            printer.printlnText("持卡人签名(CARDHOLDER SIGNATURE)");
//            if(tradeInfo.getElectronicSign() != null) {
//                printer.printBitmap(tradeInfo.getElectronicSign());
//            } else {
//                printer.printBlankLine(4);
//            }
//            printer.printDividerLine();
//        }
        printer.printlnText("本人确认以上交易，同意记入本卡账户");
        printer.printlnText("I ACKNOWLEDGE SATISFACTORY RECEIPT OF RELATIVE " +
                "GOODS/SERVICES");
        printer.printBlankLine(5);
        print(printer);
    }


    /**
     * 扫码订单打印
     *
     * @param logo
     * @param content
     * @param orderType
     * @param printPage
     * @return
     */
    public static void printPosOrder(final Bitmap logo,
                                     final String[] content,
                                     final int orderType,
                                     final int printPage) {
        switch(orderType) {
            case Constants.PRINT_LAST_ORDER:
//                printLastOrder(logo, content);
                printNewLastOrder(content);
                if(printPage == 2) {
//                    printLastOrder(logo, content);
                    printNewLastOrder(content);
                }
                break;
            case Constants.PRINT_TOTAL_ORDER:
                //交易汇总内的，优化后不需要了
                printTotalOrder(logo, content);
                if(printPage == 2) {
                    printTotalOrder(logo, content);
                }


                break;
            case Constants.PRINT_ORDER_DETAIL:
                printOrderDetail(logo, content);

                if(printPage == 2) {
                    printOrderDetail(logo, content);
                }
                break;
            case Constants.PRINT_REFUND_ORDER:
                printRefundOrder(logo, content);
                if(printPage == 2) {
                    printRefundOrder(logo, content);
                }
                break;
            case Constants.PRINT_REFUND_RESULT_ORDER:
                printRefundResultOrder(logo, content);
                if(printPage == 2) {
                    printRefundResultOrder(logo, content);
                }
                break;
            case Constants.PRINT_MEMBER_CONSUME_RESULT:
                printMemberConsumeResult(content);
                if(printPage == 2) {
                    printMemberConsumeResult(content);
                }
                break;
            case Constants.PRINT_MEMBER_RECHARGE_RESULT:
                printMemberRechargeResult(content);
                if(printPage == 2) {
                    printMemberRechargeResult(content);
                }
                break;
            case Constants.PRINT_MEMBER_RECHARGE_2:
                printMemberRecharge2(content);
                if(printPage == 2) {
                    printMemberRecharge2(content);
                }
                break;
            case Constants.PRINT_CASH_ORDER:
                printCashOrder(content);
                if(printPage == 2) {
                    printCashOrder(content);
                }
                break;
            case Constants.PRINT_SCAN_ORDER:
                //扫码订单
                printScanOrder(logo, content);
                if(printPage == 2) {
                    printScanOrder(logo, content);
                }
                break;
            case Constants.PRINT_KOUBEI:
                printKouBeiDetail(content);
                if(printPage == 2) {
                    printKouBeiDetail(content);
                }
                break;
            case Constants.PRINT_WX:
                printWXDetail(content);
                if(printPage == 2) {
                    printWXDetail(content);
                }
                break;
            case Constants.PRINT_HOTEL:
                printHotelDetail(content);
                if(printPage == 2) {
                    printHotelDetail(content);
                }
                break;
            case Constants.PRINT_GIFT:
                printGiftDetail(content);
                if(printPage == 2) {
                    printGiftDetail(content);
                }
                break;
            case Constants.PRINT_GROUP:
                printGroupDetail(content);
                if(printPage == 2) {
                    printGroupDetail(content);
                }
                break;

        }
    }

    /**
     * 扫码预授权订单打印
     *
     * @param orderType
     * @param printPage
     * @return
     */
    public static void printPreAuthoOrder(final PreAuthoOrderEntity.DataBean dataBean,
                                          final int orderType, final int printPage,boolean isAuto) {
                    switch(orderType) {
//                        case Constants.PRINT_PRE_AUTHO_0:
////                            printPreAutho01(dataBean);
//                            printNewPreAuth0(dataBean);
//                            if(printPage == 2) {
////                                printPreAutho01(dataBean);
//                                printNewPreAuth0(dataBean);
//                            }
//                            break;
                        case Constants.PRINT_PRE_AUTHO_01:
//                            printPreAutho01(dataBean);
                            printNewPreAuth01(dataBean,isAuto);
                            if(printPage == 2) {
//                                printPreAutho01(dataBean);
                                printNewPreAuth01(dataBean,isAuto);
                            }
                            break;
                        case Constants.PRINT_PRE_AUTHO_02:
//                            printPreAutho02(dataBean);
                            printNewPreAuth02(dataBean,isAuto);
                            if(printPage == 2) {
//                                printPreAutho02(dataBean);
                                printNewPreAuth02(dataBean,isAuto);
                            }
                            break;
                        case Constants.PRINT_PRE_AUTHO_03:
                            printNewPreAuth03(dataBean,isAuto);
//                            printPreAutho03(dataBean);
                            if(printPage == 2) {
//                                printPreAutho03(dataBean);
                                printNewPreAuth03(dataBean,isAuto);
                            }
                            break;
                        case Constants.PRINT_PRE_AUTHO_04:
                            printNewPreAuth04(dataBean,isAuto);
//                            printPreAutho04(dataBean);
                            if(printPage == 2) {
                                printNewPreAuth04(dataBean,isAuto);
                            }
                            break;
                    }
    }

    //预授权冻资中
    private static void printPreAutho01(PreAuthoOrderEntity.DataBean dataBean) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("订单凭据");
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), dataBean.getOrderID(),
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("订单状态:预授权冻资中");
        printer.printlnText("创建时间:" + dataBean.getCreateTime());
        printer.printlnText("预授权金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("押金金额:" + dataBean.getDepositMoney());
        printer.printlnText("付款金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("付款方式:" + (dataBean.getPayType() == 0 ? "微信" : "支付宝"));
        printer.printlnText("付款时间:" + dataBean.getPayTime());
        printer.printlnText("订单备注:" + dataBean.getCreateRemark());
        printer.printlnText("门店名称:" + dataBean.getShopName());
        printer.printlnText("收 银 员:" + dataBean.getSiteUserName());
        printer.printlnText("交 易 号:" + dataBean.getTransactionID());
        printer.printDividerLine();
        printer.printlnText("注: 此小票用于退还押金，请妥善保存！");
        printer.printlnText("打印时间:" + DayWeekMoonTImeUtil.getNowTimeString());
        printer.printBlankLine(4);
        print(printer);
    }

    //预授权完成
    private static void printPreAutho02(PreAuthoOrderEntity.DataBean dataBean) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("订单凭据");
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), dataBean.getOrderID(),
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("订单状态:预授权完成");
        printer.printlnText("退还顾客:-" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney() - dataBean.getConsumeMoney()));
        printer.printlnText("订单实收:" + dataBean.getConsumeMoney());
        printer.printlnText("完成操作:" + dataBean.getConsumeUserName());
        printer.printlnText("完成操作:" + dataBean.getConsumeUserName());
        printer.printlnText("完成时间:" + dataBean.getConsumeTime());
        printer.printlnText("完成备注:" + dataBean.getConsumeRemark());
        printer.printlnText("创建时间:" + dataBean.getCreateTime());
        printer.printlnText("预授权金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("押金金额:" + dataBean.getDepositMoney());
        printer.printlnText("付款金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("付款方式:" + (dataBean.getPayType() == 0 ? "微信" : "支付宝"));
        printer.printlnText("付款时间:" + dataBean.getPayTime());
        printer.printlnText("订单备注:" + dataBean.getCreateRemark());
        printer.printlnText("门店名称:" + dataBean.getShopName());
        printer.printlnText("收 银 员:" + dataBean.getSiteUserName());
        printer.printlnText("交 易 号:" + dataBean.getTransactionID());
        printer.printDividerLine();
        printer.printlnText("注: 此小票用于退还押金，请妥善保存！");
        printer.printlnText("打印时间:" + DayWeekMoonTImeUtil.getNowTimeString());
        printer.printBlankLine(4);
        print(printer);
    }

    //预授权撤销
    private static void printPreAutho03(PreAuthoOrderEntity.DataBean dataBean) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("订单凭据");
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("订单状态:预授权撤销");
        printer.printlnText("撤销退还:-" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("撤销操作:" + dataBean.getRefundUserName());
        printer.printlnText("撤销时间:" + dataBean.getRefundTime());
        printer.printlnText("撤销备注:" + dataBean.getRefundRemark());
        printer.printlnText("订单号:" + dataBean.getOrderID());
        printer.printlnText("创建时间:" + dataBean.getCreateTime());
        printer.printlnText("预授权金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("押金金额:" + dataBean.getDepositMoney());
        printer.printlnText("付款金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("付款方式:" + (dataBean.getPayType() == 0 ? "微信" : "支付宝"));
        printer.printlnText("付款时间:" + dataBean.getConsumeTime());
        printer.printlnText("订单备注:" + dataBean.getCreateRemark());
        printer.printlnText("门店名称:" + dataBean.getShopName());
        printer.printlnText("收 银 员:" + dataBean.getSiteUserName());
        printer.printlnText("交 易 号:" + dataBean.getTransactionID());
        printer.printDividerLine();
        printer.printlnText("注: 此小票用于退还押金，请妥善保存！");
        printer.printlnText("打印时间:" + DayWeekMoonTImeUtil.getNowTimeString());
        printer.printBlankLine(4);
        print(printer);
    }

    //预授权完成撤销
    private static void printPreAutho04(PreAuthoOrderEntity.DataBean dataBean) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("订单凭据");
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("订单状态:预授权完成撤销");
        printer.printlnText("撤销退还:-" + dataBean.getConsumeMoney());
        printer.printlnText("订单实收:0.00");
        printer.printlnText("撤销操作:" + dataBean.getRefundUserName());
        printer.printlnText("撤销时间:" + dataBean.getRefundTime());
        printer.printlnText("撤销备注:" + dataBean.getRefundRemark());
        printer.printlnText("押金消费:" + NumUtil.formatFloat(dataBean.getConsumeMoney() - dataBean.getGoodsMoney()));
        printer.printlnText("退还顾客:-:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney() - dataBean.getConsumeMoney()));
        printer.printlnText("订单实收:" + dataBean.getConsumeMoney());
        printer.printlnText("完成操作:" + dataBean.getConsumeUserName());
        printer.printlnText("完成时间:" + dataBean.getConsumeTime());
        printer.printlnText("完成备注:" + dataBean.getConsumeRemark());
        printer.printlnText("完成备注:" + dataBean.getConsumeRemark());
        printer.printlnText("订单号:" + dataBean.getOrderID());
        printer.printlnText("创建时间:" + dataBean.getCreateTime());
        printer.printlnText("预授权金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("押金金额:" + dataBean.getDepositMoney());
        printer.printlnText("付款金额:" + NumUtil.formatFloat(dataBean.getGoodsMoney() + dataBean.getDepositMoney()));
        printer.printlnText("付款方式:" + (dataBean.getPayType() == 0 ? "微信" : "支付宝"));
        printer.printlnText("付款时间:" + dataBean.getPayTime());
        printer.printlnText("订单备注:" + dataBean.getCreateRemark());
        printer.printlnText("门店名称:" + dataBean.getShopName());
        printer.printlnText("收 银 员:" + dataBean.getSiteUserName());
        printer.printlnText("交 易 号:" + dataBean.getTransactionID());
        printer.printDividerLine();
        printer.printlnText("注: 此小票用于退还押金，请妥善保存！");
        printer.printlnText("打印时间:" + DayWeekMoonTImeUtil.getNowTimeString());
        printer.printBlankLine(4);
        print(printer);
    }

    public static void printMemberOrder(final MemberConsumeDetailEntity.DataBean dataBean, MemberRechargeDetailEntity.DataBean dataBean2, final int orderType, final int printPage) {
        switch(orderType) {
            case Constants.PRINT_MEMBER_CONSUME:
                printMemberConsume(dataBean);
                if(printPage == 2) {
                    printMemberConsume(dataBean);
                }
                break;
            case Constants.PRINT_MEMBER_RECHARGE:
                printMemberRecharge(dataBean2);
                if(printPage == 2) {
                    printMemberRecharge(dataBean2);
                }
                break;

        }
    }

    private static void printMemberConsume(MemberConsumeDetailEntity.DataBean dataBean) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("会员消费");
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderId(), 320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + Hawk.get("company"));
        printer.printlnText(switchLongText("商户名称:",Hawk.get("company")));
        if(dataBean.getPayType() == 0) {
            printer.printlnText("支付方式:微信");
        } else if(dataBean.getPayType() == 1) {
            printer.printlnText("支付方式:支付宝");
        } else if(dataBean.getPayType() == 2) {
            printer.printlnText("支付方式:储值卡");
        } else if(dataBean.getPayType() == 3) {
            printer.printlnText("支付方式:云闪付");
        }
        printer.printlnText("订单金额:" + dataBean.getOrderMoney());
        printer.printlnText("商家优惠:-" + dataBean.getSellerHongBaoAmount());
        printer.printlnText("会员折扣:-" + dataBean.getLevelDiscount());
        printer.printlnText("实收金额:" + NumUtil.formatFloat(dataBean.getOrderMoney() - dataBean.getLevelDiscount() - dataBean.getSellerHongBaoAmount()));
        printer.printlnText("赠送积分:" + dataBean.getGiveIntegral());
        printer.printlnText("付款时间:" + dataBean.getPayTime());
        printer.printlnText("门店名称:" + dataBean.getShopName());
        printer.printlnText("收 银 员:" + dataBean.getSiteUserName());
        printer.printlnText("会员昵称:" + dataBean.getNickName());
        printer.printlnText("会员手机:" + dataBean.getPhone());
        printer.printlnText("备    注:" + dataBean.getRemark());
        printer.printBlankLine(4);
        print(printer);
    }

    private static void printMemberRecharge(MemberRechargeDetailEntity.DataBean dataBean) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("会员充值");
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderId(), 320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + Hawk.get("company"));
        printer.printlnText(switchLongText("商户名称:",Hawk.get("company")));
        if(dataBean.getPayType() == 0) {
            printer.printlnText("支付方式:微信");
        } else if(dataBean.getPayType() == 1) {
            printer.printlnText("支付方式:支付宝");
        } else if(dataBean.getPayType() == 2) {
            printer.printlnText("支付方式:储值卡");
        } else if(dataBean.getPayType() == 3) {
            printer.printlnText("支付方式:云闪付");
        }
        printer.printlnText("充值活动:" + (dataBean.getActivityName() == null ? "" :
                dataBean.getActivityName()));
        printer.printlnText("充值金额:" + dataBean.getRechargeMoney());
        printer.printlnText("赠送金额:" + dataBean.getGiveMoney());
        printer.printlnText("储值卡余额:" + dataBean.getHaveMoney());
        printer.printlnText("订单号:" + dataBean.getOrderId());
        printer.printlnText("付款时间:" + dataBean.getPayTime());
        printer.printlnText("门店名称:" + dataBean.getShopName());
        printer.printlnText("收 银 员:" + dataBean.getSiteUserName());
        printer.printlnText("会员昵称:" + dataBean.getMemberName());
        printer.printlnText("会员等级:" + dataBean.getMemberLevel());
        printer.printlnText("备    注:" + dataBean.getRemark());
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 交易汇总
     *
     * @param logo
     * @param content
     */
    private static void printTotalOrder(Bitmap logo, String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("门店:" + Hawk.get("shopname"));
        printer.printlnText("收银员:" + Hawk.get("sellername"));
        printer.printlnText("开始时间:" + content[38]);
        printer.printlnText("结束时间:" + content[39]);
        printer.printText("交易项", "笔数(笔)", "总金额(元)");
        printer.printlnText("商家实收      " + content[0] + "        " + content[1]);
        printer.printlnText("订单金额      " + content[2] + "        " + content[3]);
        printer.printlnText("支付宝收款    " + content[4] + "        " + content[5]);
        printer.printlnText("微信收款      " + content[6] + "        " + content[7]);
        printer.printlnText("云闪付收款    " + content[34] + "        " + content[35]);
        printer.printlnText("现金收款      " + content[10] + "        " + content[11]);
        printer.printlnText("储值卡收款    " + content[12] + "        " + content[13]);
        printer.printlnText("会员充值      " + content[32] + "        " + content[33]);
        printer.printlnText("银联收款      " + content[14] + "        " + content[15]);
        printer.printlnText("银联预授权    " + content[18] + "        " + content[19]);
        printer.printlnText("扫码预授权    " + content[36] + "        " + content[37]);
        printer.printlnText("商家优惠      " + content[22] + "        " + content[23]);
        printer.printlnText("官方优惠      " + content[24] + "        " + content[25]);
        printer.printlnText("核销金额      " + content[30] + "        " + content[31]);
        printer.printlnText("银联退款      " + content[16] + "        " + content[17]);
        printer.printlnText("扫码退款      " + content[28] + "        " + content[29]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * ·
     * 订单详情
     *
     * @param logo
     * @param content
     */
    private static void printOrderDetail(Bitmap logo, String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[6],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + content[13]);
        printer.printlnText(switchLongText("商户名称:",content[13]));
        printer.printlnText("交易类型:" + content[11]);
        printer.printlnText("交易状态:" + content[12]);
        printer.printlnText("实收金额:" + content[0]);
        printer.printlnText("交易时间:" + content[5]);
        printer.printlnText("门店名称:" + content[8]);
        printer.printlnText("收 银 员:" + content[9]);
        printer.printlnText("消 费 者:" + content[4]);
        printer.printlnText("备    注:" + content[10]);
        printer.printlnText("退款备注:" + content[14]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 打印上一笔订单
     *
     * @param logo
     * @param content
     */
    private static void printLastOrder(Bitmap logo, String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[6],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        if(!TextUtils.isEmpty(content[11])) {
            printer.printlnText(content[14]);//补打小票文案
        }
//        printer.printlnText("商户名称:" + content[13]);
        printer.printlnText(switchLongText("商户名称:",content[13]));
        printer.printlnText("交易类型:" + content[11]);
        printer.printlnText("交易状态:" + content[12]);
        printer.printlnText("实收金额:" + content[0]);
        printer.printlnText("交易时间:" + content[5]);
        printer.printlnText("门店名称:" + content[8]);
        printer.printlnText("收 银 员:" + content[9]);
        printer.printlnText("消 费 者:" + content[4]);
        printer.printlnText("备    注:" + content[10]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 打印
     *
     * @param logo
     * @param content
     */
    private static void printRefundOrder(Bitmap logo, String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[6],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);

        printer.printlnText(switchLongText("商户名称:",content[14]));
        printer.printlnText("原交易金额:" + content[0] + "元");
        printer.printlnText("退款金额:" + content[12] + "元");
        printer.printlnText("退款时间:" + content[11]);
        printer.printlnText("门店名称:" + content[8]);
        printer.printlnText("收 银 员:" + content[9]);
        printer.printlnText("消 费 者:" + content[13]);
        printer.printlnText("备    注:" + content[10]);
        printer.printlnText("退款备注:" + content[15]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 打印退款结果
     *
     * @param logo
     * @param content
     */
    private static void printRefundResultOrder(Bitmap logo, String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[6],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + content[14]);
        printer.printlnText(switchLongText("商户名称:",content[14]));
        printer.printlnText("交易类型: 退款:" + content[14]);
        printer.printlnText("原交易金额:" + content[0] + "元");
        printer.printlnText("退款金额:" + content[12] + "元");
        printer.printlnText("退款时间:" + content[11]);
        printer.printlnText("门店名称:" + content[8]);
        printer.printlnText("收 银 员:" + content[9]);
        printer.printlnText("消 费 者:" + content[13]);
        printer.printlnText("交易时间:" + content[5]);
        printer.printlnText("备    注:" + content[10]);
        printer.printlnText("退款备注:" + noNull(content[15]));
        printer.printBlankLine(4);
        print(printer);
    }

    public static void printOrderAuto(final Bitmap logo,
                                      final JSONObject jsonObject,
                                      final int printPage) {
        printAutoOrderDetail(logo, jsonObject);
        if(printPage == 2) {
            printAutoOrderDetail(logo, jsonObject);
        }
    }

    public static void printSignOut(final Bitmap logo,
                                    final List<String> printcontent) {
        printSignOutDetail(logo, printcontent);
    }

    /**
     * 主扫自动打印
     *
     * @param logo
     */
    private static void printAutoOrderDetail(Bitmap logo, JSONObject jsonObject) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                null;
        try {
            barCodeWithText = QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                    jsonObject.getString(
                            "orderId"), 320, 120, textViewConfig);
            printer.printBitmap(barCodeWithText);
            printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

            printer.printDividerLine();
            printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//            printer.printlnText("商户名称:" + jsonObject.getString(
//                    "company"));
            printer.printlnText(switchLongText("商户名称:",jsonObject.getString(
                    "company")));
            printer.printlnText("交易类型:" + jsonObject.getString(
                    "payType"));
            printer.printlnText("交易状态:" + jsonObject.getString(
                    "orderState"));
            printer.printlnText("实收金额:" + jsonObject.getString(
                    "payMoney"));
            printer.printlnText("交易时间:" + jsonObject.getString(
                    "payTime"));
            printer.printlnText("门店名称:" + jsonObject.getString(
                    "shopName"));
            printer.printlnText("收 银 员:" + jsonObject.getString(
                    "siteUserName"));
            printer.printlnText("消 费 者:" + jsonObject.getString(
                    "buyer"));
            printer.printlnText("备    注:" + jsonObject.getString(
                    "remark"));
            printer.printBlankLine(4);
            print(printer);
        } catch(JSONException e) {
            e.printStackTrace();
        }
    }

    private static void printMemberConsumeResult(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("会员" + content[0]);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + content[10]);
        printer.printlnText(switchLongText("商户名称:",content[10]));
        printer.printlnText("交易类型:会员" + content[0]);
        printer.printlnText("消费金额:" + content[1] + "元");
        printer.printlnText(content[0] + "后余额:" + content[13] + "元");
        printer.printlnText("会 员 名:" + content[5]);
        printer.printlnText("手 机 号:" + content[3]);
        printer.printlnText("会员积分:" + content[6]);
        printer.printlnText("交易时间:" + content[11]);
        printer.printlnText("门店名称:" + content[9]);
        printer.printlnText("收 银 员:" + content[8]);
        printer.printlnText("备    注:" + content[12]);
        printer.printBlankLine(4);
        print(printer);
    }

    private static void printMemberRechargeResult(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("会员" + content[0]);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + content[10]);
        printer.printlnText(switchLongText("商户名称:",content[10]));
        printer.printlnText("交易类型:会员" + content[0]);
        printer.printlnText("支付类型:" + content[12]);
        printer.printlnText(content[0] + "金额:" + content[2] + "元");
        printer.printlnText("赠送金额:" + content[3] + "元");
        printer.printlnText(content[0] + "后余额:" + content[5] + "元");
        printer.printlnText("会 员 名:" + content[6]);
        printer.printlnText("充值时间:" + content[8]);
        printer.printlnText("门店名称:" + content[7]);
        printer.printlnText("收 银 员:" + content[9]);
        printer.printlnText("备    注:");
        printer.printBlankLine(4);
        print(printer);
    }

    private static void printMemberRecharge2(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText("会员充值");
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);

        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + content[10]);
        printer.printlnText(switchLongText("商户名称:",content[10]));
        printer.printlnText("交易类型:会员充值");
        printer.printlnText("支付类型:" + content[8]);
        printer.printlnText("充值金额:" + content[2] + "元");
        printer.printlnText("赠送金额:" + content[15] + "元");
        printer.printlnText("充值后余额:" + content[13] + "元");
        printer.printlnText("会 员 名:" + content[3]);
        printer.printlnText("会员折扣:" + content[5]);
        printer.printlnText("会员积分:" + content[7]);
        printer.printlnText("充值时间:" + content[11]);
        printer.printlnText("门店名称:" + content[0]);
        printer.printlnText("收 银 员:" + content[9]);
        printer.printlnText("备    注:" + content[12]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 现金订单
     *
     * @param content
     */
    private static void printCashOrder(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(content[0]);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[1],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("收款金额(元):" + content[3]);
        printer.printDividerLine();
//        printer.printlnText("商户名称:" + content[4]);
        printer.printlnText(switchLongText("商户名称:",content[4]));
        printer.printlnText("收款方式:现金");
        printer.printlnText("收 银 员:" + content[5]);
        printer.printlnText("门    店:" + content[6]);
        printer.printlnText("备    注:" + content[8]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 扫码订单
     *
     * @param logo
     * @param content
     */
    private static void printScanOrder(Bitmap logo, String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[1],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
//        printer.printlnText("商户名称:" + content[10]);
        printer.printlnText(switchLongText("商户名称:",content[10]));
        printer.printlnText("交易类型:" + content[8]);
        printer.printlnText("实收金额:" + content[2] + "元");
        printer.printlnText("交易时间:" + content[6]);
        printer.printlnText("门店名称:" + content[5]);
        printer.printlnText("收 银 员:" + content[4]);
        printer.printlnText("消 费 者:" + content[9]);
        printer.printlnText("备    注:" + content[7]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 签退打印
     *
     * @param logo
     */
    private static void printSignOutDetail(Bitmap logo, List<String> printcontent) {
        PrinterContent printer = new PrinterContent();
        if(printcontent.size() < 5) {
            printcontent.add("暂没有数据");
        }
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.printBitmap(logo);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printBlankLine(2);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("门店:" + Hawk.get("shopname"));
        printer.printlnText("收银员:" + Hawk.get("sellername"));
        for(int i = 0; i < printcontent.size(); i++) {
            printer.printlnText(printcontent.get(i));
        }
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 口碑券核销
     *
     * @param content
     */
    private static void printKouBeiDetail(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(content[0]);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("商品名称:" + content[1]);
        printer.printlnText("商品原价:" + content[2]);
        printer.printlnText("商品现价:" + content[3]);
        printer.printlnText("实    收:" + content[4]);
        printer.printlnText("券    码:" + content[5]);
        printer.printlnText("核销时间:" + content[6]);
        printer.printlnText("核 销 人:" + content[7]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 微信卡券
     *
     * @param content
     */
    private static void printWXDetail(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(content[0]);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("卡券名称:" + content[1]);
        printer.printlnText("卡券类型:" + content[2]);
        printer.printlnText("有 效 期:" + content[3]);
        printer.printlnText("卡 券 码:" + content[4]);
        printer.printlnText("核销时间:" + content[5]);
        printer.printlnText("核 销 人:" + content[6]);
        printer.printlnText("相关说明:" + content[7]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 酒店券
     *
     * @param content
     */
    private static void printHotelDetail(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(content[0]);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("订单编号:" + content[1]);
        printer.printlnText("订单金额:" + content[2]);
        printer.printlnText("订单状态:" + content[3]);
        printer.printlnText("联系方式:" + content[4]);
        printer.printlnText("下单时间:" + content[5]);
        printer.printlnText("核销时间:" + content[6]);
        printer.printlnText("核 销 人:" + content[7]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 礼品券
     *
     * @param content
     */
    private static void printGiftDetail(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(content[0]);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("订单编号:" + content[1]);
        printer.printlnText("订单金额:" + content[2]);
        printer.printlnText("订单状态:" + content[3]);
        printer.printlnText("联系方式:" + content[4]);
        printer.printlnText("下单时间:" + content[5]);
        printer.printlnText("核销时间:" + content[6]);
        printer.printlnText("核 销 人:" + content[7]);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 团购券
     * @param content
     */
    private static void printGroupDetail(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(content[0]);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("卡券名称:" + content[1]);
        printer.printlnText("订单编号:" + content[2]);
        printer.printlnText("订单金额:" + content[3]);
        printer.printlnText("订单状态:" + content[4]);
        printer.printlnText("下单时间:" + content[5]);
        printer.printlnText("券    码:" + content[6]);
        printer.printlnText("核销时间:" + content[7]);
        printer.printlnText("核 销 人:" + content[8]);
        printer.printBlankLine(4);
        print(printer);
    }


    /**
     * 目的：商户名称太长导致无法完整打印出商户名称
     * 思路：减去“商户名称”还剩余11个汉字的空间，假设每十个字换行一次，剩余的字补加换行和9个空格（"和商户名称："对齐）
     * @return :转换后的字符串
     * @param keyText  “商户名称：” 或其他四个字
     * @param longText
     */
    private static String switchLongText(String keyText, String longText) {
        String result = keyText;
        do {
            if(longText.length() >= 10) {
                result = result + longText.substring(0, 10);
                longText = longText.substring(10);
                if(longText.length()>0){
                    result=result+ "\n          ";
                }

            } else {
                result = result + longText;
                longText = "";
            }

        } while(longText.length() > 0);

        return result;
    }

//    private static String switchLongText(String keyText, String text) {
//        String result = keyText;
//        try {
//            for(int i = 0; i < text.length(); i++) {
//                result = result + text.charAt(i);
//                if(result.getBytes("GBK").length >= 28) {
//                    if(i != text.length() - 1) {
//                        result = result + "\n";
//                    }
//                }
//            }
//
//        } catch(Exception e) {
//
//        }
//        return result;
//    }



    /**
     * 把null转为"" 不然，小票上会打印出null
     *
     * @param content
     * @return
     */
    public static String noNull(String content) {
        if(content == null || content.equals("null")) {
            return "";
        }
        return content;


    }
    //-------------------------------一下内容为小票优化后的格式---------------------------一下


    /**
     * 订单打印两行，减去左右长度，中间补空格
     *
     * @param leftText
     * @param rightText
     * @return
     */
    public static String getLeftAndRight(String leftText, String rightText) {
        StringBuilder builder = new StringBuilder();
        builder.append(leftText);
        //假设一行30个字符

        try {
            int addTextLength = 30 - leftText.getBytes("GBK").length - rightText.getBytes(
                    "GBK").length;
            for(int i = 0; i < addTextLength; i++) {
                builder.append(" ");
            }
            builder.append(rightText);
        } catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }


    /**
     * 订单打印两行第二种样式，减去左右text长度和中间固定空间剩余的补空格，剩余长度的一半补给行首
     *
     * @param leftText
     * @param rightText
     * @return
     */
    public static String getLeftAndRightTwo(String leftText, String rightText) {
        StringBuilder builder = new StringBuilder();
        //假设一行30个字符,减去中间固定的6个字符

        try {
            int addTextLength =
                    (30 - leftText.getBytes("GBK").length - rightText.getBytes(
                            "GBK").length - 6) / 2;
            for(int i = 0; i < addTextLength; i++) {
                builder.append(" ");
            }
            builder.append(leftText);
            builder.append("     ");
            builder.append(rightText);
            for(int i = 0; i < addTextLength; i++) {
                builder.append(" ");
            }
        } catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }


    public static void printNewOrder(int printPage, boolean isAuto, String[] content) {
        printNewOrderDetail(isAuto, content);
        if(printPage == 2) {
            printNewOrderDetail(isAuto, content);
        }
    }


    public static void printNewRefundOrder(int printPage, boolean isAuto,
                                           String[] content) {
        printNewRefundOrderDetail(content, isAuto);
        if(printPage == 2) {
            printNewRefundOrderDetail(content, isAuto);
        }
    }

    /**
     * 扫码订单
     *
     * @param content
     */
    public static void printNewOrderDetail(boolean isAuto, String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[0],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(isAuto) {
            printer.printlnText(content[1] + "");
        } else {
            printer.printlnText(content[1] + "(补打)");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("收 银 员: " + content[2]);
        printer.printText("支付类型: " + content[3]);
        printer.printlnText("(" + content[4] + ")");
        printer.printlnText("支付时间: " + content[5]);
        printer.printlnText(switchLongText("备    注: ", noNull(content[9])));
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        String text1 = getLeftAndRight("订单金额:", content[6]+"元");
        String text2 = getLeftAndRight("实收金额:", content[7]+"元");
        printer.printlnText(text1);
        printer.printlnText(text2);
        printer.printBlankLine(4);
        print(printer);

    }


    /**
     * 扫码订单
     *
     * @param content
     */
    public static void printJPushOrderDetail(boolean isAuto, String[] content,int page) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[0],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(isAuto) {
            printer.printlnText(content[1] + "");
        } else {
            printer.printlnText(content[1] + "(补打)");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("收 银 员: " + content[2]);
        printer.printText("支付类型: " + content[3]);
        printer.printlnText("(" + content[4] + ")");
        printer.printlnText("支付时间: " + content[5]);
        printer.printlnText(switchLongText("备    注: ", noNull(content[9])));
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        String text2 = getLeftAndRight("实收金额:", content[7]+"元");
        printer.printlnText(text2);
        printer.printBlankLine(4);
        print(printer);
        if(page ==2){
            print(printer);
        }

    }




    /**
     * 小票优化后打印退款订单
     *
     * @param content
     * @param isAuto  true 不添加补打  false  添加补打
     */
    private static void printNewRefundOrderDetail(String[] content, boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(isAuto) {
            printer.printlnText("退款订单");
        } else {
            printer.printlnText("退款订单（补打）");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText(switchLongText("门店名称: ",content[1]));
        printer.printlnText("收 银 员: " + content[2]);
        printer.printlnText("订单编号: " + content[0]);
        printer.printlnText("支付方式: " +content[3]+"(支付成功)");

        printer.printlnText("支付时间: " + content[5]);
        printer.printlnText(switchLongText("备    注: ", noNull(content[9])));
        printer.printDividerLine();
        printer.printlnText("退 款 员: " + noNull(content[17]));
        printer.printlnText("退款时间: " + content[11]);
        printer.printlnText(switchLongText("退款备注: ", noNull(content[10])));
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        String text1 = getLeftAndRight("订单金额:", content[6] + "元");
        String text2 = getLeftAndRight("实收金额:", noNull(content[7]) + "元");
        String text3 = getLeftAndRight("退款金额:", content[8] + "元");
        printer.printlnText(text1);
        printer.printlnText(text2);
        printer.printlnText(text3);
        printer.printBlankLine(4);
        print(printer);
    }


    /**
     * @param printMap 门店信息和金额数据
     * @param list     多次退款记录
     * @param isAuto   isAuto  true 不添加补打  false  添加补打
     */
    public static void printMultiRefundOrderDetail(OrderGetPosOrderLEntity.DataBean.ListBean listBean, int position,
                                                   int printPage, boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(isAuto) {
            printer.printlnText("退款订单");
        } else {
            printer.printlnText("退款订单（补打）");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText(switchLongText("门店名称: ", listBean.getShopname()));
        printer.printlnText("收 银 员: " + listBean.getSiteusername());
        printer.printlnText("订单编号: " + listBean.getOrderid());
        printer.printlnText("支付方式: " + listBean.getPaytype() + "(支付成功)");

        printer.printlnText("支付时间: " + listBean.getPaytime());
        printer.printlnText(switchLongText("备    注: ", noNull(listBean.getOrdernote())));
        printer.printDividerLine();

        if(listBean.getOrderItemRefundList() != null && listBean.getOrderItemRefundList().size() > 0) {
            if(position == -1) {
                //这种情况是打印所有
                for(int i = 0; i < listBean.getOrderItemRefundList().size(); i++) {
                    printer.printlnText("退款单号：" + listBean.getOrderItemRefundList().get(i).getRefundOrderID());
                    printer.printlnText("退款金额：" + listBean.getOrderItemRefundList().get(i).getRefundAmount() + "元");
                    printer.printlnText("退 款 员：" + noNull(listBean.getOrderItemRefundList().get(i).getRefundUserName()));
                    printer.printlnText("退款时间：" + listBean.getOrderItemRefundList().get(i).getRefundTime());
                    printer.printlnText("退款备注：" + noNull(listBean.getOrderItemRefundList().get(i).getRefundReason()));
                    printer.printDividerLine();
                }
            } else {
                //这种情况打印单条
                printer.printlnText("退款单号：" + listBean.getOrderItemRefundList().get(position).getRefundOrderID());
                printer.printlnText("退款金额：" + listBean.getOrderItemRefundList().get(position).getRefundAmount() + "元");
                printer.printlnText("退 款 员：" + noNull(listBean.getOrderItemRefundList().get(position).getRefundUserName()));
                printer.printlnText("退款时间：" + listBean.getOrderItemRefundList().get(position).getRefundTime());
                printer.printlnText("退款备注：" + noNull(listBean.getOrderItemRefundList().get(position).getRefundReason()));
                printer.printDividerLine();
            }

        } else {
            printer.printlnText("退 款 员：" + noNull(listBean.getRefundusername()));
            printer.printlnText("退款时间：" + listBean.getCreatetime());
            printer.printlnText("退款备注：" + noNull(listBean.getRefundreason()));
            printer.printDividerLine();
        }

        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        String text1 = getLeftAndRight("订单金额:", listBean.getOrdermoney() + "元");
        String text2 = getLeftAndRight("实收金额:", noNull(listBean.getRealpay()) +
                "元");
        String text3 = getLeftAndRight("退款总额:", listBean.getRefund() + "元");
        printer.printlnText(text1);
        printer.printlnText(text2);
        printer.printlnText(text3);
        printer.printBlankLine(3);
        for(int i = 0; i < printPage; i++) {
            print(printer);
        }
    }


    /**
     * 对账小票
     *小票优化后
     * @param printMap
     */
    public static void printCheckBill(HashMap<String, String> printMap,int page) {


        //粗体居中打印 用于标题和小标题
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);

        String headText = printMap.get("headText");
        printer.printlnText(headText);


        String textL1 = "营销红包"+"("+printMap.get("HongBaoCount")+")";
        String textR1 = printMap.get("HongBaoMoney");
        String textL2 = "商家折扣"+"("+printMap.get("LevelDiscountCount")+")";
        String textR2 = printMap.get("LevelDiscountMoney");
        String textL3 = "核销金额"+"("+printMap.get("CouponCount")+")";
        String textR3 = printMap.get("CouponMoney");
        String textL4 = "扫码退款"+"("+printMap.get("ScanRefundCount")+")";
        String textR4 = printMap.get("ScanRefundMoney");
        String textL5 = "银联退款"+"("+printMap.get("BankRefundCount")+")";
        String textR5 = printMap.get("BankRefundMoney");

        String textL6 = "支付宝" + "(" + printMap.get("AlipayPayCount") + "笔)";
        String textR6 = printMap.get("AlipayPayMoney");
        String textL7 = "微信" + "(" + printMap.get("WeiXinPayCount") + "笔)";
        String textR7 = printMap.get("WeiXinPayMoney");
        String textL8 = "云闪付" + "(" + printMap.get("YsfPayCount") + "笔)";
        String textR8 = printMap.get("YsfPayMoney");
        String textL9 = "银联" + "(" + printMap.get("BankPayCount") + "笔)";
        String textR9 = printMap.get("BankPayMoney");

        String textL10 = "数字人民币" + "(" + printMap.get("DigitalCount") + "笔)";
        String textR10 = printMap.get("DigitalMoney");

        String textL11 = "现金" + "(" + printMap.get("CashPayCount") + "笔)";
        String textR11 = printMap.get("CashPayMoney");
        String textL12 = "其他" + "(" + printMap.get("OtherPayCount") + "笔)";
        String textR12 = printMap.get("OtherPayMoney");

        String textL13 = "会员卡充值" + "(" + printMap.get("MemberRechargeCount") + "笔)";
        String textR13 = printMap.get("MemberRechargeMoney");
        String textL14 = "会员卡消费" + "(" + printMap.get("MemberConsumeCount") + "笔)";
        String textR14 = printMap.get("MemberConsumeMoney");
        String textL15 = "银联预授权" + "(" + printMap.get("BankDepositCount") + "笔)";
        String textR15 = printMap.get("BankDepositMoney");
        String textL16 = "扫码预授权" + "(" + printMap.get("ScanDepositCount") + "笔)";
        String textR16 = printMap.get("ScanDepositMoney");
        String textL17 = "预估手续费";
        String textR17 = printMap.get("SellerFee");

        String textOneL = "订单金额" + "(" + printMap.get("OrderCount") + "笔)";
        String textOneR = printMap.get("OrderMoney");
        String textTwoL = "商家优惠" + "(" + printMap.get("DiscountCount") + "笔)";
        String textTwoR = printMap.get("DiscountMoney");
        String textThreeL = "退款金额" + "(" + printMap.get("RefundCount") + "笔)";
        String textThreeR = printMap.get("RefundMoney");
        String textFourL = "实收金额" + "(" + printMap.get("PayCount") + "笔)";
        String textFourR = printMap.get("PayMoney");


        String text0 = "=============================" + "\n"
                + "对账时间: " + printMap.get("BeginTime") + "\n"
                + "         " + printMap.get("EndTime") + "\n"
                + switchLongText("商户信息:", printMap.get("BusinessInfo")) + "\n"
                + "门    店: " + printMap.get("Shop") + "\n"
                + "收 银 员: " + printMap.get("Seller") + "\n"
                + "打印终端: " + printMap.get("Terminal");

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText(text0);

        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        String subTitle1 = getLeftAndRightTwo(textOneL, textOneR) + "\n";
        String line1 = "————————减————————";
        String subTitle2 = getLeftAndRightTwo(textTwoL, textTwoR) + "\n";
        printer.printText(subTitle1);
        printer.printlnText(line1);
        printer.printText(subTitle2);

        String text1 = getLeftAndRight(textL1, textR1) + "\n"
                + getLeftAndRight(textL2, textR2) + "\n"
                + getLeftAndRight(textL3, textR3);

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText(text1);

        String line2 = "————————减————————";
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        String subTitle3 = getLeftAndRightTwo(textThreeL,
                textThreeR) + "\n";
        printer.printlnText(line2);
        printer.printText(subTitle3);

        String text2 = getLeftAndRight(textL4, textR4) + "\n"
                + getLeftAndRight(textL5, textR5);
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText(text2);

        String line3 = "————————等于————————";
        String subTitle4 = getLeftAndRightTwo(textFourL,
                textFourR) + "\n";
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(line3);
        printer.printText(subTitle4);


        String text3 = getLeftAndRight(textL6, textR6) + "\n"
                + getLeftAndRight(textL7, textR7) + "\n"
                + getLeftAndRight(textL8, textR8) + "\n"
                + getLeftAndRight(textL9, textR9) + "\n"
                + getLeftAndRight(textL10, textR10) + "\n"
                + getLeftAndRight(textL11, textR11) + "\n"
        +getLeftAndRight(textL12, textR12);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText(text3);
        String line4 = "———————其他金额———————";
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(line4);
        String text4 =
                getLeftAndRight(textL13, textR13) + "\n"
                + getLeftAndRight(textL14, textR14) + "\n"
                + getLeftAndRight(textL15, textR15) + "\n"
                + getLeftAndRight(textL16, textR16)+ "\n"
        + getLeftAndRight(textL17, textR17);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText(text4);
        printer.printBlankLine(4);//打印三行换行防止小票撕到文字
        print(printer);
        if(page == 2){
            print(printer);
        }

    }

    //小票优化后预授权未支付
    private static void printNewPreAuth0(PreAuthoOrderEntity.DataBean dataBean) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);

        printer.printlnText("预授权(未支付)");

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("门店名称: " + dataBean.getShopName());
        printer.printlnText("收 银 员: " + dataBean.getSiteUserName());
        printer.printlnText("交 易 号: " + dataBean.getTransactionID());
        String type = dataBean.getPayType() == 0 ? "微信" : "支付宝";
        String state = dataBean.getOrderState() == 0 ? "(未支付)" : "(支付成功)";
        printer.printlnText("支付方式: " + type + state);
        printer.printlnText("支付时间: " + dataBean.getPayTime());
        printer.printlnText(switchLongText("备    注: ", dataBean.getCreateRemark()));
        printer.printBlankLine(1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        double temp = (dataBean.getGoodsMoney() + dataBean.getDepositMoney());
        String text0 = getLeftAndRight("预授权金额:", temp + "元");
        String text1 = getLeftAndRight("押金金额:", dataBean.getDepositMoney() + "元");
        printer.printlnText(text0);
        printer.printlnText(text1);
        printer.printBlankLine(4);
        print(printer);

    }


    //小票优化后预授权冻资中
    private static void printNewPreAuth01(PreAuthoOrderEntity.DataBean dataBean,boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);

        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderID(),
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        if(isAuto){
            printer.printlnText("预授权(冻资中)");
        }else{
            printer.printlnText("预授权(冻资中)补打");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printText(switchLongText("门店名称: ", dataBean.getShopName()) + "\n");
        printer.printlnText("收 银 员: " + dataBean.getSiteUserName());
        if(dataBean.getTransactionID() != null && dataBean.getTransactionID().length()>22){
            printer.printlnText("交 易 号: " + dataBean.getTransactionID().substring(0,22));
            printer.printlnText("          " + dataBean.getTransactionID().substring(22));
        }else{
            printer.printlnText("交 易 号: " + dataBean.getTransactionID());
        }

        String type = dataBean.getPayType() == 0 ? "微信" : "支付宝";
        String state = dataBean.getOrderState() == 0 ? "(未支付)" : "(支付成功)";
        printer.printlnText("支付方式: " + type + state);
        printer.printlnText("支付时间: " + dataBean.getPayTime());
        printer.printlnText(switchLongText("备    注: ", dataBean.getCreateRemark()));
        printer.printBlankLine(1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        double temp = (dataBean.getGoodsMoney() + dataBean.getDepositMoney());
        String text0 = getLeftAndRight("预授权金额:", temp + "元");
        String text1 = getLeftAndRight("押金金额:", dataBean.getDepositMoney() + "元");
        printer.printlnText(text0);
        printer.printlnText(text1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("注:此小票用于退还押金，请妥善保存！");
        printer.printBlankLine(4);
        print(printer);


    }

    //小票优化后预授权完成
    private static void printNewPreAuth02(PreAuthoOrderEntity.DataBean dataBean,boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);

        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderID(),
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        if(isAuto){
            printer.printlnText("预授权(完成)");
        }else{
            printer.printlnText("预授权(完成)补打");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("———————①冻资中———————");
        printer.printText(switchLongText("门店名称: ", dataBean.getShopName()) + "\n");
        printer.printlnText("收 银 员: " + dataBean.getSiteUserName());
        printer.printlnText("交 易 号: " + dataBean.getTransactionID());
        String type = dataBean.getPayType() == 0 ? "微信" : "支付宝";
        String state = dataBean.getOrderState() == 0 ? "(未支付)" : "(支付成功)";
        printer.printlnText("支付方式: " + type + state);
        printer.printlnText("支付时间: " + dataBean.getPayTime());
        printer.printlnText(switchLongText("备    注: ",
                noNull(dataBean.getCreateRemark())));
        printer.printBlankLine(1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        double temp = (dataBean.getGoodsMoney() + dataBean.getDepositMoney());
        String text0 = getLeftAndRight("预授权金额:", temp + "元");
        String text1 = getLeftAndRight("押金金额:", dataBean.getDepositMoney() + "元");
        printer.printlnText(text0);
        printer.printlnText(text1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("———————②完成———————");
        printer.printlnText("退 押 员: " + dataBean.getConsumeUserName());
        printer.printlnText("退押时间: " + dataBean.getConsumeTime());
        printer.printlnText(switchLongText("退押备注: ", dataBean.getConsumeRemark()));
        printer.printBlankLine(1);

        DecimalFormat df = new DecimalFormat("0.00");
        String temp1 =
                df.format(dataBean.getGoodsMoney() + dataBean.getDepositMoney() - dataBean.getConsumeMoney()) + "元";
        String text2 = getLeftAndRight("退押金额:", temp1);
        String text3 = getLeftAndRight("实收金额:", dataBean.getConsumeMoney() + "元");
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(text2);
        printer.printlnText(text3);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("注:此小票用于退还押金，请妥善保存！");
        printer.printBlankLine(4);
        print(printer);
    }

    //小票优化后预授权撤销
    private static void printNewPreAuth03(PreAuthoOrderEntity.DataBean dataBean,Boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);

        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderID(),
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        if(isAuto){
            printer.printlnText("预授权(撤销)");
        }else{
            printer.printlnText("预授权(撤销)补打");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("——————①冻资中——————");
        printer.printText(switchLongText("门店名称: ", dataBean.getShopName()) + "\n");
        printer.printlnText("收 银 员: " + dataBean.getSiteUserName());
        printer.printlnText("交 易 号: " + dataBean.getTransactionID());
        String type = dataBean.getPayType() == 0 ? "微信" : "支付宝";
        String state = dataBean.getOrderState() == 0 ? "(未支付)" : "(支付成功)";
        printer.printlnText("支付方式: " + type + state);
        printer.printlnText("支付时间: " + dataBean.getPayTime());
        printer.printlnText(switchLongText("备    注: ", dataBean.getCreateRemark()));
        printer.printBlankLine(1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        double temp = (dataBean.getGoodsMoney() + dataBean.getDepositMoney());
        String text0 = getLeftAndRight("预授权金额:", temp + "元");
        String text1 = getLeftAndRight("押金金额:", dataBean.getDepositMoney() + "元");
        printer.printlnText(text0);
        printer.printlnText(text1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("———————③完成——————");
        printer.printlnText("撤 销 员: " + dataBean.getRefundUserName());
        printer.printlnText("撤销时间: " + dataBean.getRefundTime());
        printer.printlnText(switchLongText("撤销备注: ", dataBean.getRefundRemark()));
        printer.printBlankLine(1);
        String text5 = getLeftAndRight("撤销退还:",
                dataBean.getGoodsMoney() + dataBean.getDepositMoney() + "元");
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(text5);
        printer.printBlankLine(4);//打印三行换行防止小票撕到文字
        print(printer);
    }

    //小票优化后预授权完成撤销
    private static void printNewPreAuth04(PreAuthoOrderEntity.DataBean dataBean,boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);

        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderID(),
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);

        if(isAuto){
            printer.printlnText("预授权(完成撤销)");
        }else{
            printer.printlnText("预授权(完成撤销)补打");
        }
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("———————①冻资中———————");
        printer.printText(switchLongText("门店名称: ", dataBean.getShopName()) + "\n");
        printer.printlnText("收 银 员: " + dataBean.getSiteUserName());
        printer.printlnText("交 易 号: " + dataBean.getTransactionID());

        String type = dataBean.getPayType() == 0 ? "微信" : "支付宝";
        String state = dataBean.getOrderState() == 0 ? "(未支付)" : "(支付成功)";
        printer.printlnText("支付方式: " + type + state);
        printer.printlnText("支付时间: " + dataBean.getPayTime());
        printer.printlnText(switchLongText("备    注: ", dataBean.getCreateRemark()));
        printer.printBlankLine(1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        double temp = (dataBean.getGoodsMoney() + dataBean.getDepositMoney());
        String text0 = getLeftAndRight("预授权金额:", temp + "元");
        String text1 = getLeftAndRight("押金金额:", dataBean.getDepositMoney() + "元");
        printer.printlnText(text0);
        printer.printlnText(text1);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("———————②完成———————");
        printer.printlnText("退 押 员: " + dataBean.getConsumeUserName());
        printer.printlnText("退押时间: " + dataBean.getConsumeTime());
        printer.printlnText(switchLongText("退押备注: ", dataBean.getConsumeRemark()));
        printer.printBlankLine(1);

        DecimalFormat df = new DecimalFormat("0.00");
        String temp1 =
                df.format(dataBean.getGoodsMoney() + dataBean.getDepositMoney() - dataBean.getConsumeMoney()) + "元";
        String text2 = getLeftAndRight("退押金额:", temp1);
        String text3 = getLeftAndRight("实收金额:", dataBean.getConsumeMoney() + "元");
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(text2);
        printer.printlnText(text3);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printlnText("——————③完成撤销——————");
        printer.printlnText("撤 销 员: " + dataBean.getRefundUserName());
        printer.printlnText("撤销时间: " + dataBean.getRefundTime());
        printer.printlnText(switchLongText("撤销备注: ", dataBean.getRefundRemark()));
        printer.printBlankLine(1);
        String text5 = getLeftAndRight("撤销退还:", dataBean.getConsumeMoney() + "元");
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        printer.printlnText(text5);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 打印上一笔订单
     * 小票优化后
     */
    private static void printNewLastOrder(String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);

        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(), content[6],
                        320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(TextUtils.isEmpty(content[11])) {
            printer.printlnText(content[8]);
        } else {
            printer.printlnText(content[8] + "(补打)");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("收 银 员: " + content[9]);
        printer.printlnText("支付方式: " + content[11] + "(" + content[12] + ")");
        printer.printlnText("支付时间: " + content[5]);
        printer.printlnText(switchLongText("备    注: ", content[10]));
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        String text1 = getLeftAndRight("订单金额:", content[0]+"元");
        String text2 = getLeftAndRight("实收金额:", content[1]+"元");
        printer.printlnText(text1);
        printer.printlnText(text2);
        printer.printBlankLine(4);
        print(printer);
    }

    /**
     * 小票优化后的会员消费新方法
     *
     * @param dataBean
     */
    public static void printNewMemberConsume(int page,MemberConsumeDetailEntity.DataBean dataBean, Boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);

        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderId(), 320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(isAuto) {
            printer.printlnText(dataBean.getShopName());
        } else {
            printer.printlnText(dataBean.getShopName() + "(补打)");
        }


        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("收 银 员: " + dataBean.getSiteUserName());

        String orderState = "";
        switch(dataBean.getOrderState()) {

            case -1:
                orderState = "(已退款)";
                break;

            case 0:
                orderState = "(未支付)";
                break;

            case 1:
                orderState = "(已支付)";
                break;

        }

        if(dataBean.getPayType() == 0) {
            printer.printlnText("支付方式: 微信" + orderState);
        } else if(dataBean.getPayType() == 1) {
            printer.printlnText("支付方式: 支付宝" + orderState);
        } else if(dataBean.getPayType() == 2) {
            printer.printlnText("支付方式: 储值卡"+orderState);
        } else if(dataBean.getPayType() == 3) {
            printer.printlnText("支付方式: 云闪付"+orderState);
        }
        printer.printlnText("支付时间: " + dataBean.getPayTime());
        printer.printlnText(switchLongText("备    注: ", dataBean.getRemark()));
        printer.printDividerLine();
        printer.printlnText("会员姓名: " + dataBean.getNickName());
        //小票不管那么多只验证11位
        if(dataBean.getPhone().length() == 11) {
            StringBuffer buffer = new StringBuffer(dataBean.getPhone());
            buffer.replace(3, 7, "****");
            String newPhone = buffer.toString();
            printer.printlnText("手 机 号：" + newPhone);
        } else {
            printer.printlnText("手 机 号：" + dataBean.getPhone());
        }

        printer.printDividerLine();

        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        DecimalFormat df = new DecimalFormat("0.00");
        String temp1 = df.format(dataBean.getOrderMoney());
        String temp2 =
                df.format(dataBean.getOrderMoney() - dataBean.getLevelDiscount() - dataBean.getSellerHongBaoAmount());
        String temp3 =
                df.format(dataBean.getLevelDiscount() + dataBean.getSellerHongBaoAmount());
        String text1 = getLeftAndRight("订单金额:", temp1+"元");
        String text2 = getLeftAndRight("商家优惠: ", temp3+"元");
        String text3 = getLeftAndRight("实收金额:", temp2+"元");
        String text4 = getLeftAndRight("会员余额: ", dataBean.getHaveMoney() + "元");
        printer.printlnText(text1);
        printer.printlnText(text2);
        printer.printlnText(text3);
        printer.printlnText(text4);
        printer.printBlankLine(4);
        print(printer);
        if(page == 2){
            print(printer);
        }

    }


    /**
     * 小票优化后的会员充值新方法
     *
     * @param dataBean
     */
    public static void printNewMemberRecharge(int page,MemberRechargeDetailEntity.DataBean dataBean, Boolean isAuto) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        QRUtils.TextViewConfig textViewConfig = new QRUtils.TextViewConfig();
        textViewConfig.setSize(12);
        Bitmap barCodeWithText =
                QRUtils.getInstance().createBarCodeWithText(App.getApp(),
                        dataBean.getOrderId(), 320, 120, textViewConfig);
        printer.printBitmap(barCodeWithText);


        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(isAuto) {
            printer.printlnText("会员充值");

        } else {
            printer.printlnText("会员充值(补打)");
        }

        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.printDividerLine();
        printer.printlnText("门店名称: " + dataBean.getShopName());
        printer.printlnText("收 银 员: " + dataBean.getSiteUserName());

        String orderState = "";
        switch(dataBean.getOrderState()) {

            case -1:
                orderState = "已退款";
                break;

            case 0:
                orderState = "未支付";
                break;

            case 1:
                orderState = "已支付";
                break;

        }

        if(dataBean.getPayType() == 0) {
            printer.printlnText("支付方式: 现金(" +orderState+ ")");
        } else if(dataBean.getPayType() == 1) {
            printer.printlnText("支付方式: 微信(" + orderState + ")");
        } else if(dataBean.getPayType() == 2) {
            printer.printlnText("支付方式: 支付宝(" + orderState + ")");
        } else if(dataBean.getPayType() == 3) {
            printer.printlnText("支付方式: 云闪付(" + orderState + ")");
        }
        printer.printlnText("支付时间: " + dataBean.getPayTime());
        printer.printlnText(switchLongText("备    注: ", dataBean.getRemark()));
        printer.printDividerLine();
        printer.printlnText("会员姓名: " + dataBean.getMemberName());
        //小票不管那么多只验证11位
        if(dataBean.getMemberPhone().length() == 11) {
            StringBuffer buffer = new StringBuffer(dataBean.getMemberPhone());
            buffer.replace(3, 7, "****");
            String newPhone = buffer.toString();
            printer.printlnText("手 机 号：" + newPhone);
        } else {
            printer.printlnText("手 机 号：" + dataBean.getMemberPhone());
        }
        printer.printDividerLine();
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        DecimalFormat df = new DecimalFormat("0.00");
        String temp1 = df.format(dataBean.getRechargeMoney());
        String temp2 = df.format(dataBean.getGiveMoney());
        String temp3 = df.format(dataBean.getHaveMoney());
        String text1 = getLeftAndRight("充值金额: ", temp1+"元");
        String text2 = getLeftAndRight("赠送金额: ", temp2+"元");
        String text3 = getLeftAndRight("充值后余额: ", temp3+"元");
        printer.printlnText(text1);
        printer.printlnText(text2);
        printer.printlnText(text3);
        printer.printBlankLine(4);
        print(printer);

        if(page == 2){
            print(printer);
        }
    }



    public static void printHxDetail(int page ,Boolean isAuto,String[] content) {
        PrinterContent printer = new PrinterContent();
        printer.setAlignment(IPrinterContent.ALIGNMENT_CENTER);
        printer.setFontSize(IPrinterContent.FONT_SIZE_BIG);
        if(isAuto){
            printer.printlnText("卡券核销");
        }else{
            printer.printlnText("卡券核销(补打)");
        }
        printer.setFontSize(IPrinterContent.FONT_SIZE_NORMAL);
        printer.setAlignment(IPrinterContent.ALIGNMENT_LEFT);
        printer.printDividerLine();
        printer.printlnText("卡券名称: "+content[1]);
        printer.printlnText("卡券类型: "+content[2]);
        printer.printlnText("卡 券 码: "+content[4]);
        if(content[3] != null && content[3].split("~").length == 2) {
            printer.printlnText("有 效 期: " + content[3].split("~")[1]);
        }

        printer.printDividerLine();
        if(TextUtils.isEmpty(content[9])){
            content[9]=Hawk.get("shopname");
            if(TextUtils.isEmpty(content[9])){
                content[9] =Hawk.get("company");
            }
        }
        String temp = switchLongText("核销门店: ", noNull(content[9])) + "\n";
        printer.printText(temp);
        printer.printlnText("核 销 员: "+content[6]);
        printer.printlnText("核销时间: "+content[5]);
        if(content[2].equals("次卡")) {
            printer.printDividerLine();
            printer.printlnText("会员姓名: " + content[10]);
            if(!TextUtils.isEmpty(content[11])) {
                String pattern = "^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$";
                Boolean b = Pattern.matches(pattern, content[11]);
                if(b) {
                    StringBuffer buffer = new StringBuffer(content[11]);
                    buffer.replace(3, 7, "****");
                    String newPhone = buffer.toString();
                    printer.printlnText("手 机 号：" + newPhone);
                } else {
                    printer.printlnText("手 机 号：" + content[11]);
                }
            }

            printer.printlnText("本次您使用:");
            String temp2 = content[1] + "1次，剩余" + content[12] + "次";
            printer.printlnText(temp2);
            if(content[3] != null && content[3].split("~").length == 2) {
                String temp3 = "将于" + content[3].split("~")[1] + "失效";
                printer.printlnText(temp3);
            }
        }
        printer.printBlankLine(4);
        print(printer);
        if(page ==2){
            print(printer);
        }

    }

}
